package com.jl.sms;

import com.jl.card.JL;
import com.jl.card.util.SimpleTLV;
import com.jl.sms.userdata.UserData;
import com.jl.util.HexUtil;
import com.jl.util.LengthUtil;

public class GSMMessage implements ISMSTPDU {

	private String address = "10086";
	private int type = JL.SMS_MT_SUMMIT | JL.SMS_SM_HEADER | JL.SMS_DT_SIMDATA;
	private UserData userData;

	private String deliver = "8B XX 40 ADDRESS 7F F6 TIME bx USERDATA";
	private String submit = "8B XX 01 00 ADDRESS 7F F6 bx USERDATA";

	


	@Override
	public byte[] getTPDU(){
		//String address = GSMMessage.formatAddress(this.address);
		String res = "";
		if ((type & JL.SMS_MT_SUMMIT) == JL.SMS_MT_SUMMIT) {
			res = submit;
		} else {
			res = deliver;
			res = res.replace("TIME", new TPServiceCentreTimeStamp().valueAsString());
		}
//		res = res.replace("ADDRESS", address).replace("USERDATA",
//				this.userData.getUserDataAsValue());
		return HexUtil.parseHexToByteArray(LengthUtil.replace(res));
	}

	public static GSMMessage parse(String msg) {
		SimpleTLV tlv = SimpleTLV.fromBinary(HexUtil.parseHexToByteArray(msg),
				false);
		byte[] value = tlv.valueAsByteArray();
		int type = 0x00;
		if (value[0] == 0x40) {
			type &= ~JL.SMS_MT_SUMMIT;
		} else {
			type |= JL.SMS_MT_SUMMIT;
		}

		return null;
	}
}
